package com.cloud.async.business.context.data;

import cn.hutool.core.util.ObjectUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chengrui
 * @title: SingleEnum
 * @projectName webCloudAsync
 * @description: 单例数据源
 * @date 2021/10/13 23:15
 */
public enum SingleDataSource {

    INSTANCE;

    private  ConcurrentHashMap<String, CompletableFuture<String>> dataSource
            = new ConcurrentHashMap<>();

    SingleDataSource() {
    }

    /**
     *
     * @param initialCapacity
     * @return
     */
    public ConcurrentHashMap<String,CompletableFuture<String>> getSingleDataSource(Integer initialCapacity) {
        if (initialCapacity != null) {
            return ObjectUtil.isNotEmpty(this.dataSource) ? this.dataSource : (this.dataSource = new ConcurrentHashMap<>(initialCapacity));
        }
        return ObjectUtil.isNotEmpty(this.dataSource) ? this.dataSource : (this.dataSource = new ConcurrentHashMap<>());
    }

    /**
     *
     * @param initialCapacity
     * @param loadFactor
     * @return
     */
    public ConcurrentHashMap<String,CompletableFuture<String>> getSingleDataSource(int initialCapacity, float loadFactor) {
        return ObjectUtil.isNotEmpty(this.dataSource) ? this.dataSource : (this.dataSource = new ConcurrentHashMap<>(initialCapacity, loadFactor));
    }

    /**
     *
     * @param initialCapacity
     * @param loadFactor
     * @param concurrencyLevel
     * @return
     */
    public ConcurrentHashMap<String,CompletableFuture<String>> getSingleDataSource(int initialCapacity,
                                                                                    float loadFactor, int concurrencyLevel) {
        return ObjectUtil.isNotEmpty(this.dataSource) ? this.dataSource : (this.dataSource = new ConcurrentHashMap<>(initialCapacity, loadFactor, concurrencyLevel));
    }

}
